package controller;

import entity.UserEntity;
import frontEnd.Web;
import service.UserService;

import java.util.Scanner;

/**
 * @author HuangJH
 * @create 2023-11-30-10:59
 */
public class UserController {

    private UserService userService = new UserService();

    /**
     * 注册用户
     */
    public void register() {
        Scanner scanner = new Scanner(System.in);
        // 注册
        while (true) {
            System.out.println("==========================================");
            System.out.print("请选择您的注册身份，1-管理员 2-访客（请输入数字）：");
            System.out.println();
            int identityNum = scanner.nextInt();
            if(identityNum != 1 && identityNum != 2){
                Web.printfError(identityNum);
                continue;
            }
            System.out.print("请输入您的注册账号：");
            String account = scanner.next().trim();
            System.out.println();
            System.out.print("请输入您的注册密码：");
            String password = scanner.next().trim();
            System.out.println();
            // 注册业务
            boolean isSuccess = userService.registeredUser(identityNum, account, password);
            if(isSuccess) {
                System.out.println("注册成功");
            } else {
                System.out.println("注册失败");
            }
            break;
        }
    }

    /**
     * 登录用户
     */
    public UserEntity login() {
        Scanner scanner = new Scanner(System.in);
        // 注册
        while (true) {
            System.out.println("==========================================");
            System.out.print("请选择您的登录身份，0-返回上一级 1-管理员 2-访客（请输入数字）：");
            System.out.println();
            int firstInput = scanner.nextInt();
            if(firstInput != 0 && firstInput != 1 && firstInput != 2){
                Web.printfError(firstInput);
                continue;
            }
            if(firstInput == 0) {
                break;
            }
            System.out.print("请输入您的登录账号：");
            String account = scanner.next().trim();
            System.out.println();
            System.out.print("请输入您的登录密码：");
            String password = scanner.next().trim();
            System.out.println();
            // 登录业务
            UserEntity loginUser = userService.login(firstInput, account, password);
            if(loginUser != null) {
                System.out.println("登录成功");
                return loginUser;
            } else {
                System.out.println("登录失败");
            }
        }
        return null;
    }


}
